//
//  CollectionPageTableViewController.m
//  PetTown
//
//  Created by lanou3g on 15/6/8.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CollectionPageTableViewController.h"
#import "DatabaseHandle.h"
#import "AppDelegate.h"
#import "LeftViewControlle.h"
@interface CollectionPageTableViewController ()

@end

@implementation CollectionPageTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收藏";
    [self readData];
   // [self returns];
#pragma mark----抽屉
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"<<"style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    
}
- (void)readData{
    DatabaseHandle * handle = [DatabaseHandle shareInstance];
    PersistManager * persistM = [PersistManager shareInstance];
    //取出数据库中数据
    NSData * modelData = [handle selectActivityWithTitle:ModelKey];
    NSData * allData = [handle selectActivityWithTitle:DataKey];
    
    self.allModelArray = [persistM unarchiverObject:modelData forKey:ModelKey];
    self.allDataArray = [persistM unarchiverObject:allData forKey:DataKey];
}



#pragma mark----抽屉
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //设置changeView为隐藏
        AppDelegate * app = (AppDelegate * )[UIApplication sharedApplication].delegate;
        app.changeView.hidden = YES;
    
   
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    //清除缓存
    SDImageCache * sdI = [SDImageCache sharedImageCache];
    [sdI clearDisk];
    [sdI clearMemory];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _allModelArray.count;
}
//点击cell弹出详情
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomePageNewTableViewController * homePageNewTVC = [[HomePageNewTableViewController alloc] init];
    NSData * hh = _allDataArray[indexPath.row];
    HomePage * home = _allModelArray[indexPath.row];
    homePageNewTVC.allData = hh;
    homePageNewTVC.homePage = home;
    [self.navigationController pushViewController:homePageNewTVC animated:YES];
    
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChoicenessCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if (!cell) {
        cell = [[ChoicenessCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"mycell"];
    }
    HomePage * hh = _allModelArray[indexPath.row];
    cell.homePage = hh;
    if (hh.comments.count != 0) {
        self.ReviewerArray = [[NSMutableArray alloc] init];
        for (NSDictionary * dd in hh.comments) {
            Reviewer * rr = [[Reviewer alloc] init];
            [rr setValuesForKeysWithDictionary:dd];
            [_ReviewerArray addObject:rr];
            cell.reviewer = _ReviewerArray[0];
        }
        
    }
    
    
    return cell;
}

//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomePage * homePage = _allModelArray[indexPath.row];
    CGFloat a = self.tableView.frame.size.width * [homePage.picheight floatValue] / [homePage.picwidth floatValue] + 280;
    return a;
}
#pragma mark-----tableView编辑
//1.使处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];//先调用父类的
    [self.tableView setEditing:editing animated:YES];//让tableView处于编辑状态
    
}
//2.指定那些行可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3.设置指定分区(section)中的行(row)是什么样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//4.编辑完成(先操作数据源在修改UI)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewRowAction * rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [self.tableView beginUpdates];//同步更新beginUpdates 和 endUpdates 之间修改的UI和数据的 配对使用的
        [_allDataArray removeObjectAtIndex:indexPath.row];
        [_allModelArray removeObjectAtIndex:indexPath.row];
        //创建单例
        DatabaseHandle * handle = [DatabaseHandle shareInstance];
        PersistManager * persistM = [PersistManager shareInstance];
        //删除之前数据
        [handle deleteStudentByKey:ModelKey];
        [handle deleteStudentByKey:DataKey];
        //将数组归档
        [handle insertNewActivity:[persistM dataOfArchiverObject:_allModelArray forKey:ModelKey] Key:ModelKey];
        [handle insertNewActivity:[persistM dataOfArchiverObject:_allDataArray forKey:DataKey] Key:DataKey];
        
        //②删除UI
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        [self.tableView endUpdates];//同步更新UI和数据的 配对使用的
    }];
    NSArray * array = @[rowAction];
    return array;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
